﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ThongTinMonHoc : System.Web.UI.Page
{
    int action;
    string msmh = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageLoad();
        }
    }

    private void PageLoad()
    {
        action = int.Parse(Request.QueryString["action"]);
        if (action == 1)
        { 
        }
        if (action == 2)
        {
            msmh = Request.QueryString["MSMH"].ToString();
            if (msmh != "")
                LoadThongTin(Request.QueryString["MSMH"].ToString());
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!Không có mã số môn học');", true);
                Response.Redirect("./QuanLyMonHoc.aspx");
            }
        }
    }

    private void LoadThongTin(string p)
    {
        DataClassesDataContext data = new DataClassesDataContext();
        var mhs = from temp in data.tbl_MONHOCs
                 where temp.MsMonHoc == p
                 select temp;
        foreach (tbl_MONHOC mh in mhs)
        {
            txtTenMH.Text = mh.TenMonHoc;
            txtSoTinChi.Text = mh.SoTC.ToString();
        }
        txtMaMH.Text = p;
        txtMaMH.Enabled = false;
   }

    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        try
        {
            action = int.Parse(Request.QueryString["action"]);

            if (action == 1)
            {
                DataClassesDataContext data = new DataClassesDataContext();

                tbl_MONHOC mh = new tbl_MONHOC();
                mh.MsMonHoc = txtMaMH.Text;
                mh.TenMonHoc = txtTenMH.Text;
                mh.SoTC = int.Parse(txtSoTinChi.Text);

                data.tbl_MONHOCs.InsertOnSubmit(mh);
                data.SubmitChanges();
            }
            if (action == 2)
            {
                msmh = Request.QueryString["MSMH"].ToString();

                DataClassesDataContext data = new DataClassesDataContext();

                var SuaMH = from p in data.tbl_MONHOCs
                             where p.MsMonHoc == msmh
                             select p;

                foreach (tbl_MONHOC Sua_MH in SuaMH)
                {
                    Sua_MH.TenMonHoc = txtTenMH.Text;
                    Sua_MH.SoTC = int.Parse(txtSoTinChi.Text);
                }

                data.SubmitChanges(); 
            }
            Response.Redirect("./QuanLyMonHoc.aspx");

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!"+ex.Message.ToString()+"');", true);
        }

    }
}
